package com.softaria.spkiller.mojo;

import java.util.ArrayList;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;

import com.softaria.spkiller.analyzer.hierarchy.enclosed.EnclosedHierarchyProblemReporter;
import com.softaria.spkiller.metadata.ClassIdentifier;

public class MojoEnclosedHierarchyProblemReporter implements
		EnclosedHierarchyProblemReporter {

	private class TwoMasters {
		
		private ClassIdentifier master1;
		private ClassIdentifier master2; 
		private ClassIdentifier slave;
		
		public TwoMasters(ClassIdentifier master1, ClassIdentifier master2,
				ClassIdentifier slave) {
			super();
			this.master1 = master1;
			this.master2 = master2;
			this.slave = slave;
		}

		@Override
		public String toString() {
			return "Two masters found for one class  [master1=" + master1 + ", master2=" + master2
					+ ", slave=" + slave + "]";
		}
		
		
	}
	
	private class BrokenMasterHierarchy {
		
		private ClassIdentifier mustBeBoss;
		private ClassIdentifier mustBeSubordinate;
		private ClassIdentifier becauseOfThisSlave;
		
		public BrokenMasterHierarchy(ClassIdentifier mustBeBoss,
				ClassIdentifier mustBeSubordinate,
				ClassIdentifier becauseOfThisSlave) {
			super();
			this.mustBeBoss = mustBeBoss;
			this.mustBeSubordinate = mustBeSubordinate;
			this.becauseOfThisSlave = becauseOfThisSlave;
		}

		@Override
		public String toString() {
			return "Master hierarchy is broken [this class =" + mustBeBoss
					+ " must be boss of " + mustBeSubordinate
					+ ", because of this slave=" + becauseOfThisSlave + "]";
		}
		
		
	}
	
	List<TwoMasters> twoMasters = new ArrayList<TwoMasters>();
	List<BrokenMasterHierarchy> brokenMasterHierarchies = new ArrayList<MojoEnclosedHierarchyProblemReporter.BrokenMasterHierarchy>();
	
	
	public void reportMoreThanOneShepherd(ClassIdentifier shepherd1,
			ClassIdentifier shepherd2, ClassIdentifier sheep) {
		twoMasters.add(new TwoMasters(shepherd1,shepherd2, sheep));

	}

	public void reportBrokenShepherdHierarchy(
			ClassIdentifier mustBeBossShepherd,
			ClassIdentifier mustBeSubordinateShepherd,
			ClassIdentifier becauseOfThisSheep) {
		brokenMasterHierarchies.add(new BrokenMasterHierarchy(mustBeBossShepherd, mustBeSubordinateShepherd, becauseOfThisSheep));

	}

	public void check() throws MojoExecutionException{
		for(TwoMasters twoMaster:twoMasters) {
			System.out.println("SPAGHETTI-KILLER ERROR:  "+twoMaster.toString());
		}
		for(BrokenMasterHierarchy bh:brokenMasterHierarchies) {
			System.out.println("SPAGHETTI-KILLER ERROR:  "+bh.toString());
		}
		
		if(!twoMasters.isEmpty()||!brokenMasterHierarchies.isEmpty()) {
			throw new MojoExecutionException("See above");
		}
		
		
	}

}
